package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.AttendClassRecord;
import com.yuyou.fn.educational.entity.ShiftClassRecord;
import com.yuyou.fn.educational.mapper.AttendClassRecordMapper;
import com.yuyou.fn.educational.service.IAttendClassRecordService;
import com.yuyou.fn.educational.service.IShiftClassRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 *
 * AttendClassRecord 表数据服务层接口实现类
 *
 */
@Service
public class AttendClassRecordServiceImpl extends SuperServiceImpl<AttendClassRecordMapper, AttendClassRecord,Long> implements IAttendClassRecordService {

    @Resource
    private IShiftClassRecordService shiftClassRecordService;

    @Override
    public List<AttendClassRecord> findStudentShiftAttendClassRecord(Long regId, List<Long> classLectureIds) {
        if (regId==null || Utils.checkListEmpty(classLectureIds)){
            return Collections.EMPTY_LIST;
        }
        Example<AttendClassRecord> example = AttendClassRecord.createExample();
        example.createCriteria().andEqualTo("regId",regId).andIn("classLectureId",classLectureIds).andEqualTo("shiftStatus",1);

        return findListByExample(example);
    }

    @Override
    public void deleteShiftAttendClassRecord( List<Long> attendClassRecordIds ) {
        List<ShiftClassRecord> shiftClassRecords=shiftClassRecordService.findShiftClassRecordByFrom(attendClassRecordIds);
        List<Long> toAttendClassRecordIds = BeanUtils.toList(shiftClassRecords, "toAttendClassRecordId");
        if (Utils.checkListNotEmpty(toAttendClassRecordIds)){
            this.deleteBatchIds(toAttendClassRecordIds);
        }
    }

    @Override
    public Integer countByClassLectureId(Long classLectureId) {
        Example<AttendClassRecord> example = AttendClassRecord.createExample();
        example.createCriteria().andEqualTo("classLectureId" , classLectureId ).andIn("shiftStatus", Arrays.asList(0,2));
        return (int)countByExample(example);
    }

    @Override
    public List<AttendClassRecord> findByRegIdClassLectureIds(Long regId, List<Long> classLectureIds) {
        if(classLectureIds == null || classLectureIds.isEmpty() ){
            return Collections.EMPTY_LIST ;
        }
        Example<AttendClassRecord> example = AttendClassRecord.createExample();
        example.createCriteria().andEqualTo("regId" , regId ).andIn("classLectureId" , classLectureIds );
        return findListByExample(example);
    }

    @Override
    public List<AttendClassRecord> findByRegIdsClassLectureIds(List<Long> regIds, List<Long> classLectureIds) {
        if(Utils.isEmpty(regIds) || Utils.isEmpty(classLectureIds)){
            return Collections.EMPTY_LIST ;
        }
        Example<AttendClassRecord> example = AttendClassRecord.createExample();
        example.createCriteria().andIn("regId" , regIds ).andIn("classLectureId" , classLectureIds );
        return findListByExample(example);
    }

    @Override
    public List<AttendClassRecord> findAttendClassRecordByClassIds(List<Long> classIds) {
        Example<AttendClassRecord> example = AttendClassRecord.createExample();
        example.createCriteria().andIn("classId" , classIds );
        return findListByExample(example);
    }

    @Override
    public void clearFinalFlag(Long studentId, List<Long> classLectureIds) {
        Example<AttendClassRecord> example = AttendClassRecord.createExample();
        example.createCriteria().andEqualTo("studentId", studentId).andIn("classLectureId",classLectureIds);
        AttendClassRecord u = new AttendClassRecord() ;
        u.setFinanceFlag(0);
        updateSelectiveByExample(u,example);
    }

    @Override
    public void setUpAttendClassRemark(Long attendClassRecordId, String remark) {
        if (attendClassRecordId==null){
            throw new BusException("请传入id");
        }
        if (StringUtils.isEmpty(remark)){
            throw new BusException("备注不可为空");
        }
        AttendClassRecord attendClassRecord=new AttendClassRecord();
        attendClassRecord.setAttendClassRecordId(attendClassRecordId);
        attendClassRecord.setRemark(remark);
        this.updateSelectiveById(attendClassRecord);
    }

    @Override
    public List<AttendClassRecord> findAttendClassRecordForConsume() {
        return autoMapper.queryAttendClassRecordForConsume();
    }
}